-- 角色模块
-- Author: Colocasia
-- Date: 2018-07-30

local class = require 'class';
local handler = require 'handler';
local table = require 'us.table';


---常用函数引用
---@type fun table.merge @合并函数
local table_merge = table.merge;


---@class character @玩家数据库类
local M = class('character', handler);


---构造函数
---@param guid string @玩家GUID
function M:ctor(guid)
    self.super:ctor();
    self:init_character_data(guid);
    self:launch(guid);
end


---初始化character
---@param guid string | 'guid'
function M:init_character_data(guid)
    self.guid   = guid or '';   -- 玩家guid
    self.id     = 0;            -- 玩家id
    self.level  = 1;            -- 玩家等级
    self.exp    = 0;            -- 玩家经验
end


---生成character信息
---@param result character @角色信息
local function gen_character_info(result)
    return {
        guid        = result.guid,
        id          = result.id,
        exp         = result.exp,
    };
end


---使用GUID查询character
---@param guid string @玩家GUID
---@return table
function M:find_character_by_guid(guid)
    local result = self:exc_db_cmd('character', 'find_character_by_guid', guid);
    -- 账户不存在
    if not result then return end
    return gen_character_info(result);
end


---加载character数据
---@param guid string
---@return table
function M:launch(guid)
    local data = self:find_character_by_guid(guid);
    if not data then return end;
    table_merge({self, data});
end

return M;
